<template>
  <!--弹窗 :close-on-click-modal="false" 点击遮罩层不关闭窗口 -->
  <div>
    <!-- status-icon 当表单校验不通过时, 输入框右侧有个 x 小图标 -->
    <el-form
      :rules="rules"
      ref="formData"
      :model="formData"
      label-width="100px"
      label-position="right"
      ><el-form-item label="标题:" prop="title">
        <el-input
          v-model="formData.title"
          placeholder="请输入标题"
          maxlength="50"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="标签:" prop="labelIds">
        <!--:show-all-levels="false" 仅显示最后一级， :filterable="true" 可搜索 props{ multiple: true 多选， emitPath: false 只返回子节点的value } -->
        <el-cascader
          :disabled="disabled"
          style="display: block"
          v-model="formData.labelIds"
          :options="labelOptions"
          :show-all-levels="false"
          :props="{
            multiple: true,
            emitPath: false,
            children: 'labelList',
            value: 'id',
            label: 'name',
          }"
          :filterable="true"
          clearable
        />
      </el-form-item>
      <el-form-item label="主图:" prop="imageUrl">
        <el-upload
          class="avatar-uploader"
          accept="image/*"
          action=""
          :show-file-list="false"
          :http-request="uploadMainImg"
        >
          <img
            v-if="formData.imageUrl"
            :src="formData.imageUrl"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="是否公开:" prop="ispublic">
        <el-radio-group v-model="formData.ispublic">
          <el-radio :label="0">不公开 </el-radio
          ><el-radio :label="1">公开 </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介:" prop="summary">
        <el-input
          v-model="formData.summary"
          type="textarea"
          placeholder="请输入简介"
          :autosize="{ minRows: 3 }"
          maxlength="2000"
          show-word-
          limit
        />
      </el-form-item>
      <el-form-item label="内容:" prop="content">
        <!-- 主体内容 -->
        <mavon-editor
          :autofocus="false"
          ref="md"
          v-model="formData.mdContent"
          @change="getMdHtml"
          @imgAdd="uploadContentImg"
          @imgDel="delContentImg"
        />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="submitForm('formData')"
        >提交审核</el-button
      >
    </el-row>
  </div>
</template>
<script>
export default {
  middleware: "auth", // 引用权限中间件
  data() {
    const validateLabel = (rule, value, callback) => {
      // value获取输入框的值 // 有 value 值才校验，不然提交的时候会报错
      if (value && value.length > 5) {
        this.disabled = true;
        // 禁止点击
        callback(new Error("最多可选5个标签"));
      } else {
        // 校验通过放行
        callback();
        this.disabled = false;
      }
    }; // 万分强调：最后这里不要有逗号
    const validateContent = (rule, value, callback) => {
      if (this.formData.mdContent && this.formData.htmlContent) {
        // 有内容则放行
        callback();
      } else {
        callback(new Error("请求输入文章内容"));
      }
    }; // 不要有逗号
    return {
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        labelIds: [
          { required: true, message: "请选择标签", trigger: "blur" },
          { validator: validateLabel, trigger: "change" },
        ],
        ispublic: [
          { required: true, message: "请选择是否公开", trigger: "change" },
        ],
        summary: [{ required: true, message: "请输入简介", trigger: "blur" }],
        content: [{ validator: validateContent, trigger: "change" }],
      },
      formData: {
        imageUrl: null, // 写出来，不然图片显示不出来
        userId: this.$store.state.userInfo.uid,
        userImage: this.$store.state.userInfo.imageUrl,
        nickName: this.$store.state.userInfo.nickName,
      },
      // 表单数据
      disabled: false,
      // 标签输入框默认可输入
      labelOptions: [], // 标签下拉框
    };
  },
  async asyncData({ query, app }) {
    // 查询分类与标签
    const { data } = await app.$getCategoryAndLabel();
    return { labelOptions: data };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      // 校验通过
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.formData);
          // 校验通过，提交数据
          this.submitData();
        } else {
          // 验证不通过
          return false;
        }
      });
    },
    validate({ query }) {
      // 如果有传递id则是修改，
      if (query.id) {
        // 必须是number类型
        return /^\d+$/.test(query.id);
      }
      return true;
    },
    async asyncData({ query, app }) {
      // 查询分类与标签
      const { data } = await app.$getCategoryAndLabel();
      // 查询文章详情
      if (query.id) {
        const { data: formData } = await app.$getArticleById(query.id);
        return { labelOptions: data, formData };
      }
      return { labelOptions: data };
    },
    //  异步提交数据
    async submitData() {
      //const response = await this.$addArticle(this.formData);
      let response = null; // 有 id 值则修改，没有id则新增
      if (this.formData.id) {
        response = await this.$updateArticle(this.formData);
      } else {
        response = await this.$addArticle(this.formData);
      }
      if (response.code === 20000) {
        // 提交成功,
        this.$message.success("提交成功");
        // 跳转到详情页, data封装的就是保存后的文章id
        this.$router.push(`/article/${response.data}`);
      }
    },
    // 上传主图
    uploadMainImg(file) {
      // 封装表单对象
      const data = new FormData();
      data.append("file", file.file);
      // 发送请求上传
      this.$uploadImg(data)
        .then((response) => {
          // 以防图片多次上传，上传成功后，把原来的图片删除
          this.deleteImg();
          // 删除后赋新值
          this.formData.imageUrl = response.data;
        })
        .catch(() => {
          this.$message.error("上传失败");
        });
    },
    // 删除主图, 上传成功和关闭窗口调用删除上一次上传的图片
    deleteImg() {
      if (this.formData.imageUrl) {
        // 删除
        this.$deleteImg(this.formData.imageUrl);
      }
    },
    getMdHtml(mdContent, htmlContent) {
      this.formData.mdContent = mdContent;
      this.formData.htmlContent = htmlContent;
    },
    // 上传内容图片 (图片位置编号, File对象)
    uploadContentImg(pos, file) {
      // 第一步.将图片上传到服务器
      var fd = new FormData();
      fd.append("file", file);
      this.$uploadImg(fd).then((response) => {
        console.log(response);
        // 第二步.将返回的url替换到文本原位置! [...](0) -> ![...](url)
        this.$refs.md.$img2Url(pos, response.data);
      });
    },
    // 删除内容图片
    delContentImg(urlAndFileArr) {
      const fileUrl = urlAndFileArr[0];
      // 文件URL
      const file = urlAndFileArr[1]; // File对象
      console.log("删除内容图片", fileUrl, file);
      this.$deleteImg(fileUrl);
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>